package com.example.rentingapp.activity;

import android.content.Context;
import android.os.Message;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.adapter.CollectionAdapter;
import com.example.rentingapp.adapter.HouseCheckAdapter;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.Collection;
import com.example.rentingapp.enetity.HouseEntity;
import com.example.rentingapp.enetity.HouseResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

//查看我的房源界面
public class MyHouseCheckActivity extends BaseActivity {
    private ImageView IV_Back, IV_All_House, IV_Will_house, IV_House_Done;
    private LinearLayout LL_All_House, LL_Will_House, LL_House_done;
    private HouseCheckAdapter houseCheckAdapter = null;
    private List<HouseEntity> datas = new ArrayList<>();
    private ListView listView;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    houseCheckAdapter = new HouseCheckAdapter(datas, MyHouseCheckActivity.this);
                    listView.setAdapter(houseCheckAdapter);
                    houseCheckAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_my_house_check;
    }

    @Override
    protected void initView() {
        IV_All_House = findViewById(R.id.iv_all_house);
        IV_House_Done = findViewById(R.id.iv_house_done);
        IV_Will_house = findViewById(R.id.iv_will_house);
        LL_All_House = findViewById(R.id.ll_all_house);
        LL_Will_House = findViewById(R.id.ll_will_house);
        LL_House_done = findViewById(R.id.ll_house_done);
        IV_Back = findViewById(R.id.iv_back1);
        listView = findViewById(R.id.lv_house);
    }

    @Override
    protected void initData() {

        //跳转到房源详情
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navigateTo(HousingDetailsActivity2.class);
                insertVal("house_id",String.valueOf(datas.get(position).getHouse_id()));
                finish();
            }
        });

        //返回
        IV_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //筛选房屋
        //点击全部时
        IV_All_House.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置所有tablelayout中的Imageview
                resetImg();
                ((ImageView) LL_All_House.findViewById(R.id.iv_all_house))
                        .setImageResource(R.mipmap.all_wihte);
            }
        });

        //点击未租时
        IV_Will_house.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置所有tablelayout中的Imageview
                resetImg();
                ((ImageView) LL_Will_House.findViewById(R.id.iv_will_house))
                        .setImageResource(R.mipmap.wait_white);
            }
        });

        //点击已租时
        IV_House_Done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置所有tablelayout中的Imageview
                resetImg();
                ((ImageView) LL_House_done.findViewById(R.id.iv_house_done))
                        .setImageResource(R.mipmap.done_order_white);
            }
        });

        findHouseByInformationUserId();
    }


    //重置Imageview方法
    protected void resetImg() {
        ((ImageView) LL_All_House.findViewById(R.id.iv_all_house))
                .setImageResource(R.mipmap.all);
        ((ImageView) LL_Will_House.findViewById(R.id.iv_will_house))
                .setImageResource(R.mipmap.wait);
        ((ImageView) LL_House_done.findViewById(R.id.iv_house_done))
                .setImageResource(R.mipmap.done_order);
    }

    private void findHouseByInformationUserId() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_user_id", findByKey("user_id"));
        Api.config(ApiConfig.FIND_HOUSE_BY_House_USER_ID, params).postRequest(this, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                HouseResponse houseResponse = gson.fromJson(res, HouseResponse.class);
                if (houseResponse.getSuccess() == true) {
                    List<HouseEntity> list = houseResponse.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                }else {
                    showToastSync("暂无房源");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }


}